package com.scsj.supermarket.adapter;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.ClassifyBean;
import com.scsj.supermarket.bean.HomeClassifyBean;
import com.scsj.supermarket.event.FirstEvent;
import com.scsj.supermarket.utils.RxBus;

import java.util.List;

/**
 * 商品分类
 */

public class ClassifyAdapter extends BaseQuickAdapter<HomeClassifyBean.DataBean, BaseViewHolder> {

    public static String Tag = "分类页面";
    private int selectedPosition = 0;
    private List<HomeClassifyBean.DataBean> list;

    public ClassifyAdapter(List data) {
        super(R.layout.item_classify_layout, data);
        list = data;
        list.get(0).setSelected(true);

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelected()) {
                selectedPosition = i;
            }
        }
    }

    @Override
    protected void convert(final BaseViewHolder helper, final HomeClassifyBean.DataBean item) {

        helper.setText(R.id.name, item.getGoodsClassPt1().getName());
        helper.setChecked(R.id.name, item.isSelected());
        helper.setOnClickListener(R.id.classify_rl, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(selectedPosition).setSelected(false);
                //设置新的Item勾选状态
                selectedPosition = helper.getLayoutPosition();
                list.get(selectedPosition).setSelected(true);
                notifyDataSetChanged();
                RxBus.getDefault().post(new FirstEvent("ClassifyContentFragment",item.getAppGoodsClassPt2VoList() , item.getGoodsClassPt1().getTitle()));

            }
        });



    }
}
